﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Business.Entities;

namespace Data.DataBase
{
    public class ModuloAdapter : Data.Database.Adapter
    {
        private static List<Modulo> _Modulos;

        public static List<Modulo> Modulos
        {
            get { 
                if(_Modulos == null)
                {
                    _Modulos = new List<Modulo>();
                    Business.Entities.Modulo mod;

                    mod = new Modulo();
                    mod.Id = 1;
                    mod.Descripcion = "ABMC Inscripcion";
                    mod.State = Business.Entities.BusinessEntity.States.Unmodified;

                    _Modulos.Add(mod);

                    mod = new Modulo();
                    mod.Id = 2;
                    mod.Descripcion = "ABMC Usuarios";
                    mod.State = Business.Entities.BusinessEntity.States.Unmodified;

                    mod = new Modulo();
                    mod.Id = 3;
                    mod.Descripcion = "ABMC Materias";
                    mod.State = Business.Entities.BusinessEntity.States.Unmodified;

                    _Modulos.Add(mod);

                    _Modulos.Add(mod);
                };

                return ModuloAdapter._Modulos; 
            }
        }

        public Business.Entities.Modulo GetOne(int id)
        {
            return Modulos.Find(delegate(Modulo mod) { return mod.Id == id;});
        }

        public List<Business.Entities.Modulo> GetAll()
        {
            return new List<Modulo>(Modulos);
        }

        public void Save(Business.Entities.Modulo modulo)
        {
            switch (modulo.State)
            {
                case BusinessEntity.States.New:
                    Modulos.Add(modulo);
                    break;
                case BusinessEntity.States.Modified:
                    //Aca no se que mierda hacer, no tiene ID.
                    Modulos[Modulos.FindIndex(delegate(Modulo m) { return m.Id == modulo.Id; })] = modulo;
                    break;
                case BusinessEntity.States.Deleted:
                    this.Delete(modulo);
                    break;
            }
            modulo.State = BusinessEntity.States.Unmodified;
        }

        public void Delete(int id)
        {
            this.Delete(this.GetOne(id));
        }
        public void Delete(Business.Entities.Modulo mod) {
            Modulos.Remove(mod);
        }
    }
}
